
package org.jeecg.modules.ZcglJiecun.tesk;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.activiti.engine.delegate.DelegateExecution;
import org.jeecg.common.util.SpringContextUtils;
import org.jeecg.modules.ZcglJiecun.entity.ZcglJiecun;
import org.jeecg.modules.ZcglJiecun.service.IZcglJiecunService;
import org.jeecg.modules.activiti.entity.ActBusiness;
import org.jeecg.modules.activiti.service.Impl.ActBusinessServiceImpl;
import org.jeecg.modules.putstorage.entity.ZcglPutStorageAssets;
import org.jeecg.modules.putstorage.service.IZcglPutStorageAssetsService;
import org.jeecg.modules.putstorage.service.IZcglPutStorageService;
import org.jeecg.modules.zcgl.entity.AssetCategory;
import org.jeecg.modules.zcgl.entity.AssetFixed;
import org.jeecg.modules.zcgl.entity.AssetFixedCategory;
import org.jeecg.modules.zcgl.service.IAssetFixedCategoryService;
import org.jeecg.modules.zcgl.service.IAssetFixedMaintenanceService;
import org.jeecg.modules.zcgl.service.IAssetFixedService;
import org.jeecg.modules.zcgl.service.IAssetInfoService;
import org.jeecg.modules.zcgl.vo.AssetInfoPage;
import org.jeecg.modules.zcglRecipients.entity.ZcglRecipientsAssets;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.YearMonth;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Component
@EnableScheduling //开启定时任务
public class ReportTask {


    private IAssetFixedService assetFixedServiceImpl;

    private IAssetFixedCategoryService assetFixedCategoryServiceImpl;


    private IZcglJiecunService zcglJiecunServiceImpl;


    private IZcglPutStorageAssetsService zcglPutStorageAssetsServiceImpl;

    @Scheduled(cron = "0 0 3 1 * ?") //定时任务注解+cron表达式
    public void testScheduleTask2() {

        System.out.println("开始推送");
        if (assetFixedServiceImpl == null){
            assetFixedServiceImpl = (IAssetFixedService) SpringContextUtils.getBean("assetFixedServiceImpl");
        }
        if (assetFixedCategoryServiceImpl == null){
            assetFixedCategoryServiceImpl = (IAssetFixedCategoryService) SpringContextUtils.getBean("assetFixedCategoryServiceImpl");
        }
        if (zcglJiecunServiceImpl == null){
            zcglJiecunServiceImpl = (IZcglJiecunService) SpringContextUtils.getBean("zcglJiecunServiceImpl");
        }
        if (zcglPutStorageAssetsServiceImpl == null){
            zcglPutStorageAssetsServiceImpl = (IZcglPutStorageAssetsService) SpringContextUtils.getBean("zcglPutStorageAssetsServiceImpl");
        }




       List<AssetFixedCategory> assetFixedList = assetFixedCategoryServiceImpl.list();

        for (int a = 0; a < assetFixedList.size(); a++) {
            String ckId = assetFixedList.get(a).getId();
            String ckName = assetFixedList.get(a).getName();
            //首先循环所有的物资 对每1个物资进行处理。
            List<AssetFixed> list = assetFixedServiceImpl.list();
            for (int i = 0; i < list.size(); i++) {
                //获取每个物资
                AssetFixed assetFixed = list.get(i);
                Date date = new Date();
                Date currentDate = new Date();
                SimpleDateFormat sdf = new SimpleDateFormat("MM");
                String lastMonthStr = sdf.format(currentDate);
                int lastMonth = Integer.parseInt(lastMonthStr) - 1;
                if (lastMonth == 0) {
                    lastMonth = 12;
                }

                Calendar calendar = Calendar.getInstance();
                calendar.add(Calendar.MONTH, -1); // 减去一个月
                int year = calendar.get(Calendar.YEAR); // 获取年份
                System.out.println(year);
            String timew = year+"-"+lastMonth;

                System.out.println("开始推送");


                // 定义日期格式
                DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM");

                // 解析日期字符串
                YearMonth yearMonth = YearMonth.parse(timew, formatter);

                // 减去一个月
                YearMonth lastMonth1 = yearMonth.minusMonths(1);

                // 输出上一个月的年和月份
                int lastYear = lastMonth1.getYear();
                int lastMonthNumber = lastMonth1.getMonthValue();

                // 输出结果
                System.out.println("上一个月的年月是：" + lastYear + "-" + String.format("%02d", lastMonthNumber));
                String shangyue = lastYear + "-" + String.format("%02d", lastMonthNumber);





//                String timew = "2023-12";
                System.out.println(timew+"当前日期11"+assetFixed.getCode());

                //去获取上个月的结存单价和结存数量

                ZcglJiecun zcglJiecun = zcglJiecunServiceImpl.getOne(new QueryWrapper<ZcglJiecun>().eq("time",timew).eq("asset_code",assetFixed.getCode()).eq("ck_id",ckId));
                System.out.println(zcglJiecun+"低价");
                ZcglJiecun shangyuejiecun = zcglJiecunServiceImpl.getOne(new QueryWrapper<ZcglJiecun>().eq("time",shangyue).eq("asset_code",assetFixed.getCode()).eq("ck_id",ckId));



                if (zcglJiecun == null) {
                    System.out.println(assetFixed.getPrice()+"");
                    //上月结存数量
                    Double count = 0.0;
                    //上月结存单价
                    Double price = 0.0;
                    if(shangyuejiecun!=null){
                        count = shangyuejiecun.getCount();
//						 price = zcglJiecun.getPrice();
                    }
                    BigDecimal total = new BigDecimal(0.0);
                    BigDecimal lCount = new BigDecimal(0.0);
                    BigDecimal lyCount = new BigDecimal(0.0);
                    List<ZcglPutStorageAssets> list1 = zcglJiecunServiceImpl.getRkList(assetFixed.getCode(),timew,ckId);
                    for (int j = 0; j < list1.size(); j++) {

                        //本月入库金额
                        total =  list1.get(j).getAssetPrice().multiply(list1.get(j).getAssetAmount()).add(total);

                        //本月入库数量
                        lCount = list1.get(j).getAssetAmount().add(lCount);
                    }

                    List<ZcglRecipientsAssets> list2 = zcglJiecunServiceImpl.getListByCk(assetFixed.getCode(),ckId,timew);
                    for (int v = 0; v < list2.size(); v++) {
                        //本月领用数量
                        lyCount = list2.get(v).getAssetAmount().add(lyCount);
                    }
                    Double benyuekucun = count.doubleValue()+lCount.doubleValue()-lyCount.doubleValue();
                    System.out.println("本月结存是多少"+benyuekucun);
                    Double jiecundanjia = 0.0;
//					 if(benyuekucun==0){
//						 jiecundanjia = 0.0;
//
//					 }else{
//						 jiecundanjia = ((price*count)+total.doubleValue())/benyuekucun;
                    AssetFixed assetFixed1 = assetFixedServiceImpl.getOne(new QueryWrapper<AssetFixed>().eq("asset_fixed_category_id",ckId).eq("code",assetFixed.getCode()));
                    if(assetFixed1!=null){
//							benyuekucun = assetFixed1.getAmount().doubleValue();
                        jiecundanjia = assetFixed1.getPrice().doubleValue();

                    }

//					 }

                    ZcglJiecun zcglJiecun1 = new ZcglJiecun();
                    zcglJiecun1.setAssetCode(assetFixed.getCode());
                    zcglJiecun1.setAssetName(assetFixed.getName());
                    zcglJiecun1.setRkCount(lCount.doubleValue());
                    zcglJiecun1.setCkCount(lyCount.doubleValue());
                    zcglJiecun1.setAssetId(assetFixed.getId());
                    zcglJiecun1.setTime(timew);
                    DecimalFormat df = new DecimalFormat("#.00");

                    zcglJiecun1.setPrice(Double.parseDouble(df.format(jiecundanjia)));
                    zcglJiecun1.setCount(benyuekucun);
                    zcglJiecun1.setCkId(ckId);
                    zcglJiecun1.setCk(ckName);
                    zcglJiecun1.setType("2");

                    if(shangyuejiecun!=null){

                        zcglJiecun1.setShangyueJiecun(shangyuejiecun.getCount());
                    }else{
                        zcglJiecun1.setShangyueJiecun(0.0);

                    }
                    System.out.println(zcglJiecun1);
                    if(zcglJiecun1.getRkCount()!=0 ||
                            zcglJiecun1.getCkCount()!=0 ||
                            zcglJiecun1.getShangyueJiecun()!=0
                    ){
                        zcglJiecunServiceImpl.save(zcglJiecun1);
                    }

                }

            }



        }




    }

//
//    @Scheduled(cron = "0/20 * * * * ?") //定时任务注解+cron表达式
//    public void testScheduleTask1() {
//
//        System.out.println("开始推送");
//        if (assetFixedServiceImpl == null){
//            assetFixedServiceImpl = (IAssetFixedService) SpringContextUtils.getBean("assetFixedServiceImpl");
//        }
//        if (zcglJiecunServiceImpl == null){
//            zcglJiecunServiceImpl = (IZcglJiecunService) SpringContextUtils.getBean("zcglJiecunServiceImpl");
//        }
//        if (zcglPutStorageAssetsServiceImpl == null){
//            zcglPutStorageAssetsServiceImpl = (IZcglPutStorageAssetsService) SpringContextUtils.getBean("zcglPutStorageAssetsServiceImpl");
//        }
//        //首先循环所有的物资 对每1个物资进行处理。
//        List<AssetFixed> list = assetFixedServiceImpl.list();
//        for (int i = 0; i < list.size(); i++) {
//            //获取每个物资
//            AssetFixed assetFixed = list.get(i);
//            Date date = new Date();
//            Date currentDate = new Date();
//            SimpleDateFormat sdf = new SimpleDateFormat("MM");
//            String lastMonthStr = sdf.format(currentDate);
//            int lastMonth = Integer.parseInt(lastMonthStr) - 1;
//            if (lastMonth == 0) {
//                lastMonth = 12;
//            }
//
//            Calendar calendar = Calendar.getInstance();
//            calendar.add(Calendar.MONTH, -1); // 减去一个月
//            int year = calendar.get(Calendar.YEAR); // 获取年份
//            System.out.println(year);
////            String timew = year+"-"+lastMonth;
//            String timew = "2023-11";
//            System.out.println(timew+"当前日期11"+assetFixed.getCode());
//
//            //去获取上个月的结存单价和结存数量
//
//            ZcglJiecun zcglJiecun = zcglJiecunServiceImpl.getOne(new QueryWrapper<ZcglJiecun>().eq("time",timew).eq("asset_code",assetFixed.getCode()));
//
//            if (zcglJiecun == null) {
//
//
//                //上月结存数量
//                Double count = 0.0;
//                //上月结存单价
//                Double price = 0.0;
//                if(zcglJiecun!=null){
//                    count = zcglJiecun.getCount();
//                    price = zcglJiecun.getPrice();
//                }
//                BigDecimal total = new BigDecimal(0.0);
//                BigDecimal lCount = new BigDecimal(0.0);
//                BigDecimal lyCount = new BigDecimal(0.0);
//                List<ZcglPutStorageAssets> list1 = zcglPutStorageAssetsServiceImpl.list(new QueryWrapper<ZcglPutStorageAssets>().eq("asset_code",assetFixed.getCode()).eq("DATE_FORMAT(create_time, '%Y-%m')",timew));
//                for (int j = 0; j < list1.size(); j++) {
//
//                    //本月入库金额
//                    total =  list1.get(j).getAssetPrice().multiply(list1.get(j).getAssetAmount()).add(total);
//
//                    //本月入库数量
//                    lCount = list1.get(j).getAssetAmount().add(lCount);
//                }
//
//                List<ZcglRecipientsAssets> list2 = zcglJiecunServiceImpl.getList(assetFixed.getCode(),timew);
//                for (int v = 0; v < list2.size(); v++) {
//                    //本月领用数量
//                    lyCount = list2.get(v).getAssetAmount().add(lyCount);
//                }
//                Double benyuekucun = count.doubleValue()+lCount.doubleValue()-lyCount.doubleValue();
//                System.out.println("本月结存是多少"+benyuekucun);
//                Double jiecundanjia = 0.0;
//                if(benyuekucun==0){
//                    jiecundanjia = 0.0;
//                }else{
//                    jiecundanjia = ((price*count)+total.doubleValue())/benyuekucun;
//                }
//
//
//                ZcglJiecun zcglJiecun1 = new ZcglJiecun();
//                zcglJiecun1.setAssetCode(assetFixed.getCode());
//                zcglJiecun1.setAssetName(assetFixed.getName());
//                zcglJiecun1.setRkCount(lCount.doubleValue());
//                zcglJiecun1.setCkCount(lyCount.doubleValue());
//                zcglJiecun1.setAssetId(assetFixed.getId());
//                zcglJiecun1.setTime(timew);
//                DecimalFormat df = new DecimalFormat("#.00");
//
//                zcglJiecun1.setPrice(Double.parseDouble(df.format(jiecundanjia)));
//                zcglJiecun1.setCount(benyuekucun);
//                System.out.println(zcglJiecun1);
//                zcglJiecunServiceImpl.save(zcglJiecun1);
//            }
//
//        }


//    }

}
